package com.basic.java.threads.stop;

/**
 * 类
 * 创建时间：2022/4/18 22:25
 *
 * @author gejin
 */
public class StopThread implements Runnable {
    private boolean flag = true;
    int i = 0;

    @Override
    public void run() {
        while (flag) {
            System.out.println(Thread.currentThread().getName() + "--->" + i++);
        }
    }

    public void stop() {
        this.flag = false;
    }

    public static void main(String[] args) {
        StopThread stopThread = new StopThread();
        new Thread(stopThread).start();
        for (int i = 0; i < 1000; i++) {
            System.out.println("main 线程正在运行---->" + i);
            if (i == 950) {
                stopThread.stop();
                System.out.println("线程停止了");
            }
        }
    }
}
